package org.practice.dao;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.practice.model.Category;
import org.practice.util.DaoException;
import org.practice.util.HibernateSessionFactory;

public class CategoryDaoImpl implements CategoryDao{
	private static Logger logger=Logger.getLogger(CategoryDaoImpl.class);
		public Category findCategoryById(Integer id) {
			Session session=HibernateSessionFactory.openSession();
			Transaction tr=null;
			Category category=null;
			try {
				tr=session.beginTransaction();
//				category=(Category)session.createQuery("from Category c where c.id=?").setLong(0, id).uniqueResult();
				category=(Category)session.get(Category.class, id);
				if (category == null) {
					throw new DaoException("�޷��ҵ�ָ��Id�ķ���");
				}
				tr.commit();
				session.close();
			} catch (HibernateException e) {
				tr.rollback();
				logger.error("��ݷ��ʳ��?",e);
				throw new DaoException("��ݷ��ʳ���",e);
			}
			return category;
		}

}
